/**
 * Created by 苑永志 on 2015/11/17.
 */
(function($,window){
    var crosstab = {}

    var key = "zm_delay_data",
        terminate_flag = 'mz_terminate____',
        cross_tab_key_flag = 'mz_cross_tab_key_flag____',
        listenCallback,
        terminateCallback;

    crosstab.registerCrosstab = function(){
        var cross_tab_keys;
        try{
            cross_tab_keys = JSON.parse(localStorage[cross_tab_key_flag]);
        }catch (e){
            cross_tab_keys = [];
        }

        var key = Date.now();
        cross_tab_keys.push(key);

        localStorage[cross_tab_key_flag] = JSON.stringify(cross_tab_keys);

        return key;
    };

    crosstab.logoutCrosstab = function(cross_tab_key){
        var cross_tab_keys;
        try{
            cross_tab_keys = JSON.parse(localStorage[cross_tab_key_flag]);
        }catch (e){
            return;
        }

        if(cross_tab_keys.length == 0){
            return;
        }

        for(var i in cross_tab_keys){
            if(cross_tab_keys[i] === cross_tab_key){
                cross_tab_keys.splice(i,1);
                break;
            }
        }

        localStorage[cross_tab_key_flag] = JSON.stringify(cross_tab_keys);
    };

    crosstab.clearCrosstab = function(){
        localStorage.removeItem(cross_tab_key_flag);
    };

    crosstab.hasCrosstab = function(){
        var cross_tab_keys;
        try{
            cross_tab_keys = JSON.parse(localStorage[cross_tab_key_flag]);
        }catch (e){
            return false;
        }
        return cross_tab_keys && cross_tab_keys.length > 0;
    };

    // send a message to all open tabs; including this tab
    crosstab.sendMessage = function(message) {
        // wrap the message in a relay object
        var relay = {
            message: message,
            timestamp: Date.now()
        };

        try {
            localStorage[key] = JSON.stringify(relay);
        } catch(e) {
            console.error("Tried to send non-json-able message:", message);
            throw e;
        }

        // fire the event locally
        //crosstab.trigger("relay", message);
    };

    crosstab.sendTerminateMessage = function(){
        crosstab.sendMessage(terminate_flag);
    };

    function storageFun(e) {
        if(e.key == key) {
            var mz_relay = JSON.parse(e.newValue),
                message = mz_relay.message;
            switch(message){
                case terminate_flag :
                    crosstab.terminate();
                    break;
                default:
                    listenCallback(message);
            }
        }
    }

    crosstab.listen = function(callback) {
        if(typeof callback == 'function'){
            listenCallback = callback;
        }
        // listen to storage events
        window.addEventListener('storage',storageFun,false);
    };

    crosstab.terminate = function(){
        if(typeof terminateCallback == 'function'){
            terminateCallback();
        }
        terminateCallback = null;
        window.removeEventListener('storage',storageFun,false);
    };

    crosstab.onterminate = function(callback){
        if(callback && typeof callback == 'function'){
            terminateCallback = callback;
        }else {
            console.error('callback supposed to be a function: ',callback);
            throw e;
        }
    };

    $.crosstab = crosstab;
})($,window);
